//爬取鲜花网列表页数据，转存到数据库中，并且图片下载到本地 : http://www.r9it.com/20171106/puppeteer.html 高级爬虫
//下载图片
const request = require("request"); //发起请求，第三方
const fs = require("fs"); //核心模块
let path = require("path"); //处理路径的模块，核心模块
let cheerio = require("cheerio");
var iconv = require("iconv-lite"); //转码的模块
let mongo = require("./db/mongo");

function ranDom(min, max) {
  return parseInt(Math.random() * (max - min + 1)) + min;
}

//改写：获取10页数据
for (let i = 1; i <= 5; i++) {
  request(
    {
      url: "https://www.hua.com/aiqingxianhua/?r=0&pg=" + i,
      encoding: "utf-8",
    },
    async (err, res, body) => {
      // console.log(body);
      iconv.decode(body, "gb2312"); //i解码
      let $ = cheerio.load(body, { decodeEntities: false });
      //分析节点，抽取下要的内容，存起来
      let arr = []; //[{},{}]
      // let leg = $(".grid-wrapper .grid-item").length;
      // console.log(leg, "数据的条数");
      $(".grid-wrapper .grid-item").each((index, item) => {
        let url = "http:" + $(item).find(".img-box img").attr("src"); //获取图片路径：使用对方的资源路径(有失败的风险，某天他把图片删除)
        let filename = path.basename(url); //获取路径后面的文件名
        request(url).pipe(fs.createWriteStream("./img/" + filename));
        url = "http://localhost:2004/img/" + filename;
        //   $(item).find(".grid-panel .price .price-num").html("666");
        //   let price = $(item).find(".grid-panel div span.price-num").html(); //获取价格
        let price = ranDom(100, 1000); //价格在100-1000以内
        let title = $(item).find(".product-title").html(); //获取标题
        let feature = $(item).find(".feature").html();
        let obj = { url, price, title, feature };
        arr.push(obj);
      });

      console.log(arr);
      //把爬取下来的数据存入到数据库中
      let data = await mongo.create("goodinf", arr);
      console.log(data);
    }
  );
}
